Skip to content

OptionalWidget

Base classes

Name Children Inherits
GroupBox
prettyqt.widgets.groupbox
Group box frame with a title.

⋔ Inheritance diagram

graph TD
  2172583965["custom_widgets.OptionalWidget"]
  595380733["widgets.GroupBox"]
  829357528["widgets.WidgetMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  625385617["gui.PaintDeviceMixin"]
  2026038775["QtWidgets.QGroupBox"]
  3620195106["QtWidgets.QWidget"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  673956333["QtGui.QPaintDevice"]
  595380733 --> 2172583965
  829357528 --> 595380733
  3227531497 --> 829357528
  2751680898 --> 3227531497
  625385617 --> 829357528
  2751680898 --> 625385617
  2751680898 --> 829357528
  2026038775 --> 595380733
  3620195106 --> 2026038775
  3984060476 --> 3620195106
  37984305 --> 3984060476
  2751680898 --> 37984305
  673956333 --> 3620195106
  37984305 --> 673956333

🛈 DocStrings

Bases: GroupBox

Wraps another widget in a GroupBox with and CheckBock and makes it optional.

Source code in prettyqt/custom_widgets/optionalwidget.py
class OptionalWidget(widgets.GroupBox):
    """Wraps another widget in a GroupBox with and CheckBock and makes it optional."""

    value_changed = core.Signal(object)

    def __init__(
        self,
        widget: ValueWidget,
        title: str = "",
        parent: widgets.QWidget | None = None,
    ):
        super().__init__(checkable=True, title=title)
        self.set_layout("vertical")
        self.box.add(widget)
        self.widget = widget
        self.toggled.connect(self.widget.setEnabled)
        self.widget.value_changed.connect(self._on_value_change)
        self.toggled.connect(self._on_enable_change)

    def _on_value_change(self, val):
        self.value_changed.emit(val)

    def _on_enable_change(self, val):
        self.widget.setEnabled(val)
        if val:
            self.value_changed.emit(self.widget.get_value())
        else:
            self.value_changed.emit(None)

    def __getattr__(self, value: str):
        return getattr(self.widget, value)

    @classmethod
    def setup_example(cls):
        w = widgets.CheckBox("Example")
        return cls(widget=w)

    @property
    def enabled(self) -> bool:
        return self.isChecked()

    @enabled.setter
    def enabled(self, state: bool):
        self.setChecked(state)

    def get_value(self):
        return self.widget.get_value() if self.isChecked() else None

:fa-brands-github: Show source on GitHub